﻿using Microsoft.Extensions.DependencyInjection;
using mozhi.smarterp.Finance.Basedatas;
using mozhi.smarterp.Finance.Basedatas.Repository;
using mozhi.smarterp.Finance.Localization;
using mozhi.smarterp.Finance.Logs.Services;
using mozhi.smarterp.Finance.Querys;
using Mozhi.Abp.Domain.BasicData;
using Mozhi.Abp.Domain.BasicData.Repositories;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Volo.Abp;
using Volo.Abp.Application.Services;

namespace mozhi.smarterp.Finance;

public abstract class FinanceAppService : ApplicationService
{
    protected IDataAssociationRepository _dataAssociationRepository;
    protected IOrganizationRepository _organizationRepository;

    public OperationLogManager _operationLogManager;
    protected FinanceAppService()
    {
        LocalizationResource = typeof(FinanceResource);
        ObjectMapperContext = typeof(FinanceApplicationModule);
    }

    protected FinanceAppService(IDataAssociationRepository dataAssociationRepository)
    {
        LocalizationResource = typeof(FinanceResource);
        ObjectMapperContext = typeof(FinanceApplicationModule);
        _dataAssociationRepository = dataAssociationRepository;
    }

    protected FinanceAppService(IServiceProvider serviceProvider)
    {
        LocalizationResource = typeof(FinanceResource);
        ObjectMapperContext = typeof(FinanceApplicationModule);
        _operationLogManager = serviceProvider.GetService<OperationLogManager>();
        _dataAssociationRepository = serviceProvider.GetService<IDataAssociationRepository>();
    }

    protected FinanceAppService(IOrganizationRepository organizationRepository)
    {
        _organizationRepository = organizationRepository;
    }

    public async Task CheckDataAssociation(Guid occupiedId)
    {
        var dataAssociations = await _dataAssociationRepository.FindAsync(new DataAssociationQuery() { OccupiedId = occupiedId });

        if (dataAssociations.Count == 0)
            return;

        var groups = dataAssociations.GroupBy(x => x.OccupantType);

        var messages = new List<string>();

        foreach (IGrouping<string, DataAssociation> info in groups)
        {
            var details = info.Select(x => x.OccupantCode + "-" + x.OccupantName).ToList();

            var tips = info.First().OccupantTypeName + ":" + string.Join(",", details);

            messages.Add(tips);
        }

        throw new UserFriendlyException($"存在数据引用:{string.Join(";", messages)},无法操作", "100000");
    }

    protected List<DimensionGroupValueItem> GetDimensionGroupValueItems(List<DimensionGroupValueItemDto> items)
    {
        var result = new List<DimensionGroupValueItem>();

        if (items == null || items.Count == 0)
            return result;

        foreach (var item in items)
        {
            var resultItem = new DimensionGroupValueItem(Guid.NewGuid(), Guid.NewGuid(), item.DataSourceType, item.TagId,
            item.Value?.ToString());
            result.Add(resultItem);
        }

        return result;
    }

    protected virtual async Task<Organization> GetOrganizationAsync()
    {
        var organization = await _organizationRepository.GetAsync();

        return organization;
    }
}

